package es.mgil.whoWantsToBeAMillonaire.runnables;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import android.content.Context;
import android.os.Message;
import es.mgil.whoWantsToBeAMillonaire.handlers.AddFriendHandler;

public class AddFriendRunnable implements Runnable {
	public static final int ADD_FRIEND_ERROR_EMPTY = 0;
	public static final int ADD_FRIEND_ERROR_SERVER = 1;
	public static final int ADD_FRIEND_OK = 2;

	private AddFriendHandler handler;
	private String myName;
	private String friendName;

	public AddFriendRunnable(Context context, String myName, String friendName) {
		this.myName = myName;
		this.friendName = friendName;
		this.handler = new AddFriendHandler(context);
	}

	@Override
	public void run() {
		try {
			// create the parameters for the connection, indicating a timeout
			HttpParams params = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(params, 3000);
			HttpConnectionParams.setSoTimeout(params, 5000);

			// create the connection to the external service
			HttpClient client = new DefaultHttpClient(params);
			HttpPost request = new HttpPost(
					"http://soletaken.disca.upv.es:8080/CMU/rest/friends");
			List<NameValuePair> pairs = new ArrayList<NameValuePair>();
			pairs.add(new BasicNameValuePair("name", myName));
			pairs.add(new BasicNameValuePair("friend_name", friendName));
			request.setEntity(new UrlEncodedFormEntity(pairs));
			client.execute(request);
			handler.sendMessage(Message.obtain(handler, ADD_FRIEND_OK));
		} catch (ClientProtocolException e) {
			// an error occurred while trying to connect to the server
			handler.sendMessage(Message
					.obtain(handler, ADD_FRIEND_ERROR_SERVER));
		} catch (UnsupportedEncodingException e) {
			// an error occurred while trying to connect to the server
			handler.sendMessage(Message
					.obtain(handler, ADD_FRIEND_ERROR_SERVER));
		} catch (IOException e) {
			// an error occurred while trying to connect to the server
			handler.sendMessage(Message
					.obtain(handler, ADD_FRIEND_ERROR_SERVER));
		}
	}

	/**
	 * Add a friend to the external server using an http connection launched in
	 * another thread
	 */
	public void addFriend() {
		if (myName.trim().equals("") || friendName.trim().equals("")) {
			// only add it if the name is not empty
			handler.sendMessage(Message.obtain(handler, ADD_FRIEND_ERROR_EMPTY));
		} else {
			Thread thread = new Thread(this);
			thread.start();
		}
	}
}
